package com.fh.controller.system.role;

import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.fh.entity.system.User;
import com.fh.util.*;
import com.thoughtworks.xstream.mapper.Mapper;
import net.sf.json.JSONArray;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.entity.system.Menu;
import com.fh.entity.system.Role;
import com.fh.service.system.menu.MenuService;
import com.fh.service.system.role.RoleService;

import static com.fh.controller.system.tools.GetDatalayui.getLData;
import static com.fh.controller.system.tools.GetDatalayui.getPData;

/**
 * 类名称：RoleController
 * 创建人：FH 
 * 创建时间：2014年6月30日
 * @version
 */
@Controller
@RequestMapping(value="/role")
public class RoleController extends BaseController {
	
	@Resource(name="menuService")
	private MenuService menuService;
	@Resource(name="roleService")
	private RoleService roleService;

	/**
	 * 角色列表(json)
	 */
	@RequestMapping(value = "/json")
	@ResponseBody
	public Object json(Page page) {
		PageData pd = new PageData();
		Map<String, Object> map = new HashMap<String, Object>();
		List<PageData> varList;
		String datalayui = null;

		//获取当前登录用户
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user = (User) session.getAttribute(Const.SESSION_USER);

		try {
			//测试分页 导出为layui的json数据
			pd = this.getPageData();
			pd.put("USER_ID", user.getUSER_ID());
			getPData(page, pd);//必填 2参
			varList = roleService.listAllRoles(page);    //列出Building列表
			datalayui =getLData(page, varList);//必填 2参
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			logAfter(logger);
		}
		return datalayui;
	}


	/**
	 * 权限列表
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/Authoritylist")
	@ResponseBody
	public PageData list() {
		logBefore(logger, "权限列表二级菜单");
		PageData res = new PageData();
		PageData pd = this.getPageData();

		try {
			List<PageData> list = roleService.listERAuthority(pd);

			res.put("data", list);
			res.put("errcode", "0");
			res.put("errmsg", "ok");
		} catch (Exception e) {
			logger.error(e.toString(), e);
			res.put("data", "");
			res.put("errcode", "1");
			res.put("errmsg", e.toString());
		}

		return res;
	}


	/**
	 * 权限列表
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/Authoritylistone")
	@ResponseBody
	public PageData listt() {
		logBefore(logger, "权限列表二级菜单");
		PageData res = new PageData();
		PageData pd = this.getPageData();

		try {
			List<PageData> list = roleService.listERAuthorityone(pd);

			res.put("data", list);
			res.put("errcode", "0");
			res.put("errmsg", "ok");
		} catch (Exception e) {
			logger.error(e.toString(), e);
			res.put("data", "");
			res.put("errcode", "1");
			res.put("errmsg", e.toString());
		}

		return res;
	}






	/**
	 * 权限列表
	 * @param page
	 * @return
	 */
	/*
	@RequestMapping(value = "/listjs")
	@ResponseBody
	public PageData listjs(Page page) {
		logBefore(logger, "Authority列表");
		PageData res = new PageData();
		PageData pd = this.getPageData();
		try {
			List<PageData> list = roleService.listERAuthority(page);
			res.put("data", list);
			res.put("errcode", "0");
			res.put("errmsg", "ok");
		} catch (Exception e) {
			logger.error(e.toString(), e);
			res.put("data", "");
			res.put("errcode", "1");
			res.put("errmsg", e.toString());
		}
		return res;
	}*/


	/**
	 * 角色新增
	 * @return
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public PageData save() {
		logBefore(logger, "新增Business");
		PageData res = new PageData();
		PageData pd = this.getPageData();

		//获取当前登录用户
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user = (User) session.getAttribute(Const.SESSION_USER);
		String id=user.getUSER_ID();
		try {
			String ROLE_CODE ="";
				if (pd.get("ly")!=null &&!"".equals(pd.get("ly"))){
					ROLE_CODE += pd.get("ly") ;
				}if (pd.get("room")!=null &&!"".equals(pd.get("room"))) {
					ROLE_CODE += pd.get("room");
				}if (pd.get("zd")!=null &&!"".equals(pd.get("zd"))){
					ROLE_CODE += pd.get("zd");
				}if (pd.get("ht")!=null &&!"".equals(pd.get("ht"))) {
					ROLE_CODE += pd.get("ht");
				}if (pd.get("wy")!=null &&!"".equals(pd.get("wy"))) {
					ROLE_CODE += pd.get("wy");
				}if (pd.get("sq")!=null &&!"".equals(pd.get("sq"))) {
					ROLE_CODE += pd.get("sq");
				}if (pd.get("fyadd")!=null &&!"".equals(pd.get("fyadd"))) {
					ROLE_CODE += pd.get("fyadd");
				}if (pd.get("fybj")!=null &&!"".equals(pd.get("fybj"))) {
					ROLE_CODE += pd.get("fybj");
				}if (pd.get("fydel")!=null &&!"".equals(pd.get("fydel"))) {
					ROLE_CODE += pd.get("fydel");
				}if (pd.get("fjadd")!=null &&!"".equals(pd.get("fjadd"))) {
					ROLE_CODE += pd.get("fjadd");
				}if (pd.get("fjbj")!=null &&!"".equals(pd.get("fjbj"))) {
					ROLE_CODE += pd.get("fjbj");
				}if (pd.get("fjdel")!=null &&!"".equals(pd.get("fjdel"))) {
					ROLE_CODE += pd.get("fjdel");
				}if (pd.get("zdbj")!=null &&!"".equals(pd.get("zdbj"))) {
					ROLE_CODE += pd.get("zdbj");
				}if (pd.get("qrfk")!=null &&!"".equals(pd.get("qrfk"))) {
					ROLE_CODE += pd.get("qrfk");
				}if (pd.get("zdadd")!=null &&!"".equals(pd.get("zdadd"))) {
					ROLE_CODE += pd.get("zdadd");
				}if (pd.get("zddel")!=null &&!"".equals(pd.get("zddel"))) {
					ROLE_CODE += pd.get("zddel");
				}if (pd.get("htck")!=null &&!"".equals(pd.get("htck"))) {
					ROLE_CODE += pd.get("htck");
				}if (pd.get("htlu")!=null &&!"".equals(pd.get("htlu"))) {
					ROLE_CODE += pd.get("htlu");
				}if (pd.get("htbj")!=null &&!"".equals(pd.get("htbj"))) {
					ROLE_CODE += pd.get("htbj") ;
				}if (pd.get("xzgl")!=null &&!"".equals(pd.get("xzgl"))) {
					ROLE_CODE += pd.get("xzgl") ;
				}if (pd.get("tzgl")!=null &&!"".equals(pd.get("tzgl"))) {
					ROLE_CODE += pd.get("tzgl");
				}if (pd.get("htdel")!=null &&!"".equals(pd.get("htdel"))) {
					ROLE_CODE += pd.get("htdel");
				}if (pd.get("fyck")!=null &&!"".equals(pd.get("fyck"))) {
					ROLE_CODE += pd.get("fyck");
				}if (pd.get("zdck")!=null &&!"".equals(pd.get("zdck"))) {
					ROLE_CODE += pd.get("zdck");
				}
			ROLE_CODE=ROLE_CODE.substring(0,ROLE_CODE.length()-1);

			pd.put("ID", this.get32UUID());    //主键
			pd.put("IS_DEL", 0); //是否删除
			pd.put("SORT", 0); //排序
			//pd.put("CREATE_ID", user.getUSER_ID()); //创建时间
			pd.put("CREATE_DATE", new DateUtil().getTime()); //创建时间

			pd.put("ROLE_CODE",ROLE_CODE);
			pd.put("CREATE_ID",id);
			roleService.add(pd);
			res.put("data", "");
			res.put("errcode", "0");
			res.put("errmsg", "ok");
		} catch (Exception e) {
			logger.error(e.toString(), e);
			res.put("data", "");
			res.put("errcode", "1");
			res.put("errmsg", e.toString());
		}
		return res;
	}

	/**
	 * 跳到权限修改-新
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/FindById")
	@ResponseBody
	public PageData listone() {
		logBefore(logger, "Authority列表");
 		PageData res = new PageData();
		PageData pd = this.getPageData();
		try {
			System.out.print(pd.get("ID"));
			PageData list = roleService.findbyid(pd);
			res.put("data", list);
			res.put("errcode", "0");
			res.put("errmsg", "ok");
		} catch (Exception e) {
			logger.error(e.toString(), e);
			res.put("data", "");
			res.put("errcode", "1");
			res.put("errmsg", e.toString());
		}
		return res;
	}

	/**
	 * 角色修改-新
	 * @return
	 */
	@RequestMapping(value = "/editx")
	@ResponseBody
	public PageData editx() {
		logBefore(logger, "新增Business");
		PageData res = new PageData();
		PageData pd = this.getPageData();
		//获取当前登录用户
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user = (User) session.getAttribute(Const.SESSION_USER);
		String CREATE_ID = user.getUSER_ID();
		try {
			String ROLE_CODE ="";
			if (pd.get("ly")!=null &&!"".equals(pd.get("ly"))){
				ROLE_CODE += pd.get("ly") ;
			}if (pd.get("room")!=null &&!"".equals(pd.get("room"))) {
				ROLE_CODE += pd.get("room");
			}if (pd.get("zd")!=null &&!"".equals(pd.get("zd"))){
				ROLE_CODE += pd.get("zd");
			}if (pd.get("ht")!=null &&!"".equals(pd.get("ht"))) {
				ROLE_CODE += pd.get("ht");
			}if (pd.get("wy")!=null &&!"".equals(pd.get("wy"))) {
				ROLE_CODE += pd.get("wy");
			}if (pd.get("sq")!=null &&!"".equals(pd.get("sq"))) {
				ROLE_CODE += pd.get("sq");
			}if (pd.get("fyadd")!=null &&!"".equals(pd.get("fyadd"))) {
				ROLE_CODE += pd.get("fyadd");
			}if (pd.get("fybj")!=null &&!"".equals(pd.get("fybj"))) {
				ROLE_CODE += pd.get("fybj");
			}if (pd.get("fydel")!=null &&!"".equals(pd.get("fydel"))) {
				ROLE_CODE += pd.get("fydel");
			}if (pd.get("fjadd")!=null &&!"".equals(pd.get("fjadd"))) {
				ROLE_CODE += pd.get("fjadd");
			}if (pd.get("fjbj")!=null &&!"".equals(pd.get("fjbj"))) {
				ROLE_CODE += pd.get("fjbj");
			}if (pd.get("fjdel")!=null &&!"".equals(pd.get("fjdel"))) {
				ROLE_CODE += pd.get("fjdel");
			}if (pd.get("zdbj")!=null &&!"".equals(pd.get("zdbj"))) {
				ROLE_CODE += pd.get("zdbj");
			}if (pd.get("qrfk")!=null &&!"".equals(pd.get("qrfk"))) {
				ROLE_CODE += pd.get("qrfk");
			}if (pd.get("zdadd")!=null &&!"".equals(pd.get("zdadd"))) {
				ROLE_CODE += pd.get("zdadd");
			}if (pd.get("zddel")!=null &&!"".equals(pd.get("zddel"))) {
				ROLE_CODE += pd.get("zddel");
			}if (pd.get("htck")!=null &&!"".equals(pd.get("htck"))) {
				ROLE_CODE += pd.get("htck");
			}if (pd.get("htlu")!=null &&!"".equals(pd.get("htlu"))) {
				ROLE_CODE += pd.get("htlu");
			}if (pd.get("htbj")!=null &&!"".equals(pd.get("htbj"))) {
				ROLE_CODE += pd.get("htbj") ;
			}if (pd.get("xzgl")!=null &&!"".equals(pd.get("xzgl"))) {
				ROLE_CODE += pd.get("xzgl") ;
			}if (pd.get("tzgl")!=null &&!"".equals(pd.get("tzgl"))) {
				ROLE_CODE += pd.get("tzgl");
			}if (pd.get("htdel")!=null &&!"".equals(pd.get("htdel"))) {
				ROLE_CODE += pd.get("htdel");
			}if (pd.get("fyck")!=null &&!"".equals(pd.get("fyck"))) {
				ROLE_CODE += pd.get("fyck");
			}if (pd.get("zdck")!=null &&!"".equals(pd.get("zdck"))) {
				ROLE_CODE += pd.get("zdck");
			}
			ROLE_CODE=ROLE_CODE.substring(0,ROLE_CODE.length()-1);
			pd.put("ROLE_CODE",ROLE_CODE);
			pd.put("CREATE_ID",CREATE_ID);
			roleService.edit(pd);
			res.put("data", "");
			res.put("errcode", "0");
			res.put("errmsg", "ok");
		} catch (Exception e) {
			logger.error(e.toString(), e);
			res.put("data", "");
			res.put("errcode", "1");
			res.put("errmsg", e.toString());
		}
		return res;
	}

    /**
     * 角色的删除
     */
    @RequestMapping(value ="/deleterole")
    @ResponseBody
    public PageData delete() {
        logBefore(logger, "删除role");
        PageData res = new PageData();
        PageData pd = this.getPageData();
        try {
            pd = this.getPageData();
            roleService.delete(pd);
            res.put("data", "");
            res.put("errcode", "0");
            res.put("errmsg", "ok");
        } catch (Exception e) {
            logger.error(e.toString(), e);
            res.put("data", "");
            res.put("errcode", "1");
            res.put("errmsg", e.toString());
        }
        return res;
    }


	/**
	 * 权限(增删改查)
	 */
	@RequestMapping(value="/qx")
	public ModelAndView qx()throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			String msg = pd.getString("msg");
			roleService.updateQx(msg,pd);
			mv.setViewName("save_result");
			mv.addObject("msg","success");
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/**
	 * K权限
	 */
	@RequestMapping(value="/kfqx")
	public ModelAndView kfqx()throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			String msg = pd.getString("msg");
			roleService.updateKFQx(msg,pd);
			
			mv.setViewName("save_result");
			mv.addObject("msg","success");
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/**
	 * c权限
	 */
	@RequestMapping(value="/gysqxc")
	public ModelAndView gysqxc()throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			String msg = pd.getString("msg");
			roleService.gysqxc(msg,pd);
			
			mv.setViewName("save_result");
			mv.addObject("msg","success");
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/**
	 * 列表
	 */
	@RequestMapping
	public ModelAndView list(Page page)throws Exception{
			ModelAndView mv = this.getModelAndView();
			PageData pd = new PageData();
			pd = this.getPageData();
			
			String roleId = pd.getString("ROLE_ID");
			if(roleId == null || "".equals(roleId)){
				pd.put("ROLE_ID", "1");
			}
			List<Role> roleList = roleService.listAllRoles();				//列出所有部门
			List<Role> roleList_z = roleService.listAllRolesByPId(pd);		//列出此部门的所有下级
			
			List<PageData> kefuqxlist = roleService.listAllkefu(pd);		//管理权限列表
			List<PageData> gysqxlist = roleService.listAllGysQX(pd);		//用户权限列表
			pd = roleService.findObjectById(pd);							//取得点击部门
			mv.addObject("pd", pd);
			mv.addObject("kefuqxlist", kefuqxlist);
			mv.addObject("gysqxlist", gysqxlist);
			mv.addObject("roleList", roleList);
			mv.addObject("roleList_z", roleList_z);
			mv.setViewName("system/role/role_list");
			mv.addObject(Const.SESSION_QX,this.getHC());	//按钮权限
		
		return mv;
	}








	/**
	 * 新增页面
	 */
	@RequestMapping(value="/toAdd")
	public ModelAndView toAdd(Page page){
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			mv.setViewName("system/role/role_add");
			mv.addObject("pd", pd);
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/**
	 * 保存新增信息
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public ModelAndView add()throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			
			String parent_id = pd.getString("PARENT_ID");		//父类角色id
			pd.put("ROLE_ID", parent_id);			
			if("0".equals(parent_id)){
				pd.put("RIGHTS", "");
			}else{
				String rights = roleService.findObjectById(pd).getString("RIGHTS");
				pd.put("RIGHTS", (null == rights)?"":rights);
			}

			pd.put("QX_ID", "");
			
			String UUID = this.get32UUID();
			
				pd.put("GL_ID", UUID);
				pd.put("FX_QX", 0);				//发信权限
				pd.put("FW_QX", 0);				//服务权限
				pd.put("QX1", 0);				//操作权限
				pd.put("QX2", 0);				//产品权限
				pd.put("QX3", 0);				//预留权限
				pd.put("QX4", 0);				//预留权限
				roleService.saveKeFu(pd);		//保存到K权限表
			
				pd.put("U_ID", UUID);
				pd.put("C1", 0);				//每日发信数量
				pd.put("C2", 0);
				pd.put("C3", 0);
				pd.put("C4", 0);
				pd.put("Q1", 0);				//权限1
				pd.put("Q2", 0);				//权限2
				pd.put("Q3", 0);
				pd.put("Q4", 0);
				roleService.saveGYSQX(pd);		//保存到G权限表
				pd.put("QX_ID", UUID);
			
			pd.put("ROLE_ID", UUID);
			pd.put("ADD_QX", "0");
			pd.put("DEL_QX", "0");
			pd.put("EDIT_QX", "0");
			pd.put("CHA_QX", "0");
			roleService.add(pd);
			mv.addObject("msg","success");
		} catch(Exception e){
			logger.error(e.toString(), e);
			mv.addObject("msg","failed");
		}
		mv.setViewName("save_result");
		return mv;
	}
	
	/**
	 * 请求编辑
	 */
	@RequestMapping(value="/toEdit")
	public ModelAndView toEdit( String ROLE_ID )throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			pd.put("ROLE_ID", ROLE_ID);
			pd = roleService.findObjectById(pd);
			
			mv.setViewName("system/role/role_edit");
			mv.addObject("pd", pd);
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/**
	 * 编辑
	 */
	@RequestMapping(value="/edit")
	public ModelAndView edit()throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			pd = roleService.edit(pd);
			mv.addObject("msg","success");
		} catch(Exception e){
			logger.error(e.toString(), e);
			mv.addObject("msg","failed");
		}
		mv.setViewName("save_result");
		return mv;
	}
	
	/**
	 * 请求角色菜单授权页面
	 */
	@RequestMapping(value="/auth")
	public String auth(@RequestParam String ROLE_ID,Model model)throws Exception{
		
		try{
			List<Menu> menuList = menuService.listAllMenu();
			Role role = roleService.getRoleById(ROLE_ID);
			String roleRights = role.getRIGHTS();
			if(Tools.notEmpty(roleRights)){
				for(Menu menu : menuList){
					menu.setHasMenu(RightsHelper.testRights(roleRights, menu.getMENU_ID()));
					if(menu.isHasMenu()){
						List<Menu> subMenuList = menu.getSubMenu();
						for(Menu sub : subMenuList){
							sub.setHasMenu(RightsHelper.testRights(roleRights, sub.getMENU_ID()));
						}
					}
				}
			}
			JSONArray arr = JSONArray.fromObject(menuList);
			String json = arr.toString();
			json = json.replaceAll("MENU_ID", "id").replaceAll("MENU_NAME", "name").replaceAll("subMenu", "nodes").replaceAll("hasMenu", "checked");
			model.addAttribute("zTreeNodes", json);
			model.addAttribute("roleId", ROLE_ID);
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		
		return "authorization";
	}
	
	/**
	 * 请求角色按钮授权页面
	 */
	@RequestMapping(value="/button")
	public ModelAndView button(@RequestParam String ROLE_ID,@RequestParam String msg,Model model)throws Exception{
		ModelAndView mv = this.getModelAndView();
		try{
			List<Menu> menuList = menuService.listAllMenu();
			Role role = roleService.getRoleById(ROLE_ID);
			
			String roleRights = "";
			if("add_qx".equals(msg)){
				roleRights = role.getADD_QX();
			}else if("del_qx".equals(msg)){
				roleRights = role.getDEL_QX();
			}else if("edit_qx".equals(msg)){
				roleRights = role.getEDIT_QX();
			}else if("cha_qx".equals(msg)){
				roleRights = role.getCHA_QX();
			}
			
			if(Tools.notEmpty(roleRights)){
				for(Menu menu : menuList){
					menu.setHasMenu(RightsHelper.testRights(roleRights, menu.getMENU_ID()));
					if(menu.isHasMenu()){
						List<Menu> subMenuList = menu.getSubMenu();
						for(Menu sub : subMenuList){
							sub.setHasMenu(RightsHelper.testRights(roleRights, sub.getMENU_ID()));
						}
					}
				}
			}
			JSONArray arr = JSONArray.fromObject(menuList);
			String json = arr.toString();
			//System.out.println(json);
			json = json.replaceAll("MENU_ID", "id").replaceAll("MENU_NAME", "name").replaceAll("subMenu", "nodes").replaceAll("hasMenu", "checked");
			mv.addObject("zTreeNodes", json);
			mv.addObject("roleId", ROLE_ID);
			mv.addObject("msg", msg);
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		mv.setViewName("system/role/role_button");
		return mv;
	}
	
	/**
	 * 保存角色菜单权限
	 */
	@RequestMapping(value="/auth/save")
	public void saveAuth(@RequestParam String ROLE_ID,@RequestParam String menuIds,PrintWriter out)throws Exception{
		PageData pd = new PageData();
		try{
			if(null != menuIds && !"".equals(menuIds.trim())){
				BigInteger rights = RightsHelper.sumRights(Tools.str2StrArray(menuIds));
				Role role = roleService.getRoleById(ROLE_ID);
				role.setRIGHTS(rights.toString());
				roleService.updateRoleRights(role);
				pd.put("rights",rights.toString());
			}else{
				Role role = new Role();
				role.setRIGHTS("");
				role.setROLE_ID(ROLE_ID);
				roleService.updateRoleRights(role);
				pd.put("rights","");
			}
				
				pd.put("roleId", ROLE_ID);
				roleService.setAllRights(pd);
			
			out.write("success");
			out.close();
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
	}
	
	/**
	 * 保存角色按钮权限
	 */
	@RequestMapping(value="/roleButton/save")
	public void orleButton(@RequestParam String ROLE_ID,@RequestParam String menuIds,@RequestParam String msg,PrintWriter out)throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		try{
			if(null != menuIds && !"".equals(menuIds.trim())){
				BigInteger rights = RightsHelper.sumRights(Tools.str2StrArray(menuIds));
				pd.put("value",rights.toString());
			}else{
				pd.put("value","");
			}
			pd.put("ROLE_ID", ROLE_ID);
			roleService.updateQx(msg,pd);
			
			out.write("success");
			out.close();
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value="/delete")
	@ResponseBody
	public Object deleteRole(@RequestParam String ROLE_ID)throws Exception{
		Map<String,String> map = new HashMap<String,String>();
		PageData pd = new PageData();
		String errInfo = "";
		try{
			pd.put("ROLE_ID", ROLE_ID);
			List<Role> roleList_z = roleService.listAllRolesByPId(pd);		//列出此部门的所有下级
			if(roleList_z.size() > 0){
				errInfo = "false";
			}else{
				
				List<PageData> userlist = roleService.listAllUByRid(pd);
				List<PageData> appuserlist = roleService.listAllAppUByRid(pd);
				if(userlist.size() > 0 || appuserlist.size() > 0){
					errInfo = "false2";
				}else{
				roleService.deleteRoleById(ROLE_ID);
				roleService.deleteKeFuById(ROLE_ID);
				roleService.deleteGById(ROLE_ID);
				errInfo = "success";
				}
			}
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		map.put("result", errInfo);
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/* ===============================权限================================== */
	public Map<String, String> getHC(){
		Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>)session.getAttribute(Const.SESSION_QX);
	}
	/* ===============================权限================================== */
	

}
